package com.iw.democorner;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;

public class MainDiag extends JDialog {
	private MainDiag mDialog;
	private final JPanel contentPanel = new JPanel();
	private JLabel iwLabel;
	private JLabel dianxinLabel;
	private JLabel wandoujiaLabel;
	private JLabel youmengLabel;
	private JLabel motuLabel;
	private JLabel xingyunLabel;
	private JLabel leaLabel;
	private JLabel yingyonghuiLabel;
	private JLabel budingLabel;
	private JLabel leiyouLabel;
	private JLabel diandianLabel;
	private JLabel zhihuLabel;
	private JLabel closeLabel;
	private JPanel panel;
	private JTable projTable;
	private JPanel panel_2;
	private JButton btnNewButton;
	private JProgressBar progressBar;

	private ProjInfo[][] projData = null;

	private int ROW_COUNT = 2;
	private int COLUMN_COUNT = 8;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			MainDiag dialog = new MainDiag();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public MainDiag() {
		mDialog = this;
		initProjIcons();
		addProjects();

		getContentPane().setBackground(new Color(255, 248, 220));
		setUndecorated(true);
		setResizable(false);
		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		Toolkit tk = Toolkit.getDefaultToolkit();
		setSize((int) tk.getScreenSize().getWidth(), (int) tk.getScreenSize()
				.getHeight());
		contentPanel.setBackground(new Color(255, 239, 213));
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));

		getContentPane().setLayout(new BorderLayout(0, 0));
		getContentPane().add(contentPanel, BorderLayout.CENTER);

		contentPanel.setLayout(new BorderLayout(0, 100));
		ImgTableModel model = new ImgTableModel();

		projTable = new JTable(model);
		projTable.setRowHeight(tk.getScreenSize().height / (ROW_COUNT + 1));
		for (int i = 0; i < COLUMN_COUNT; i++) {
			projTable.getColumnModel().getColumn(i)
					.setCellRenderer(new ImageRenderer());
		}

		projTable.setShowVerticalLines(false);
		projTable.setShowHorizontalLines(false);
		projTable.setShowGrid(false);
		projTable.setEnabled(false);
		projTable.setBackground(new Color(255, 239, 213));
		contentPanel.add(projTable, BorderLayout.CENTER);

		panel = new JPanel();
		panel.setBackground(new Color(255, 239, 213));
		getContentPane().add(panel, BorderLayout.NORTH);
		panel.setLayout(new BorderLayout(0, 150));

		closeLabel.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				mDialog.dispose();
			}
		});
		panel.add(closeLabel);
		closeLabel.setToolTipText("\u5173\u95ED");
		panel.add(iwLabel, BorderLayout.SOUTH);

		panel_2 = new JPanel();
		getContentPane().add(panel_2, BorderLayout.SOUTH);

		progressBar = new JProgressBar();
		progressBar.setSize(tk.getScreenSize().width / 2,
				tk.getScreenSize().width / 10);
		progressBar.setBounds(0, 0, 100, 100);
		panel_2.add(progressBar);

		btnNewButton = new JButton("New button");
		panel_2.add(btnNewButton);
	}

	public void initProjIcons() {

	}

	
	private void setProject(int row,int column,String name){
		projData[row][column].name=name;
		projData[row][column].icon = new ImageIcon("inf/"+name+"/"+name+".png");
	}
	
	public void addProjects() {
		projData = new ProjInfo[ROW_COUNT][COLUMN_COUNT];
		for(int i=0;i<COLUMN_COUNT;i++)
			for(int j=0;j<ROW_COUNT;j++)
				projData[j][i]=new ProjInfo();

		ImageIcon iwIcon = new ImageIcon("inf/iw/iw.png");
		iwLabel = new JLabel("", iwIcon, JLabel.CENTER);

		ImageIcon closeIcon = new ImageIcon("inf/iw/close.png");
		closeLabel = new JLabel("", closeIcon, SwingConstants.RIGHT);

		int startRow=0;
		setProject(startRow,2,"dianxin");
		setProject(startRow,3,"wandoujia");
		setProject(startRow,4,"youmeng");
		setProject(startRow,5,"motu");
		setProject(startRow,6,"xingyun");
		startRow++;
		setProject(startRow,1,"lea");
		setProject(startRow,2,"yingyonghui");
		setProject(startRow,3,"buding");
		setProject(startRow,4,"leiyou");
		setProject(startRow,5,"diandian");
		setProject(startRow,6,"zhihu");
	}

	class ImageRenderer extends DefaultTableCellRenderer {
		JLabel lbl = new JLabel();
		ImageIcon icon = null;

		ImageRenderer() {
			// icon = new ImageIcon("inf/leiyou/leiyou.png");
		}

		public Component getTableCellRendererComponent(JTable table,
				Object value, boolean isSelected, boolean hasFocus, int row,
				int column) {
			lbl.setText((String) value);
			lbl.setIcon(resizeImg(projData[row][column].icon));
			lbl.setHorizontalAlignment(SwingConstants.CENTER);
			return lbl;
		}
		
		private ImageIcon resizeImg(ImageIcon srcIcon){
			if(srcIcon==null)
				return null;
		    Image img = srcIcon.getImage();
		    Image scaledImage = img.getScaledInstance((int)(srcIcon.getIconWidth()*1.5),
		    		(int)(srcIcon.getIconHeight()*1.5),
		    		Image.SCALE_DEFAULT);
		    return new ImageIcon(scaledImage);
		}
	}

	class ImgTableModel extends AbstractTableModel {
		public Object getValueAt(int row, int column) {
			return "";
		}

		public int getColumnCount() {
			return COLUMN_COUNT;
		}

		public int getRowCount() {
			return ROW_COUNT;
		}
	};

	public static class ProjInfo {
		String name;
		String intro;
		String url;
		Image[] screenshots;
		String apkPath;
		Image qrCode;
		Image urlImg;
		
		JLabel label;
		ImageIcon icon;
	}
}
